<?php

namespace app\api\controller;

use think\Exception;
use \think\Request;
use \think\Db;
use think\Session;
use think\Config;
use think\Controller;

class Order extends Base
{
    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-16
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-(订单处理,订单首页,订单搜索)
     * @param:
     * @return json
     */
    public function myOrder()
    {
        $post = Request::instance()->post();
        if (!empty($post['type']) && !empty($post['user_id'])) {
            if ($post['type'] == 1) {                 //订单处理(1)
                $where = ['O.user_id' => $post['user_id'], 'O.status' => 1];
            } elseif ($post['type'] == 3) {           //订单首页(待确认2,3)
                //$where = ['O.user_id' => $post['user_id'], 'O.status' => 3];
				$where['O.user_id'] = $post['user_id'];
				$where['O.status'] = [
					['=',2],
					['=',3],
					'or'
				];

            } elseif ($post['type'] == 4) {           //订单搜索(已完成4,5,7)
                $where = ['O.user_id' => $post['user_id']];
            } elseif ($post['type'] == 6) {           //订单搜索(已取消6)
                $where = ['O.user_id' => $post['user_id'], 'O.status' => 6];
            }
            if ($post['type'] == 4 || $post['type'] == 6){
                $order = $this->orderInfo($post['user_id'], $post['type']);
                if ($order) {
                    $result = Config::get('code.success');
                    $result['order'] = $order['data'];
                    $result['count'] = $order['count'];
                } else {
                    $result = Config::get('code.null');
                }
                return json($result);
            }
            $date = date("Y-m-d-W", time());
            $date = explode('-', $date);
            for ($i = 0; $i < 4; $i++) {
                $week = date("Y-W", mktime(0, 0, 0, $date[1], $date[2] + $i * 7, $date[0]));
                $week = explode('-', $week);
                $time[] = $this->weekday($week[0], $week[1]);
            }
            $order = [];
            foreach ($time as $k=>$v){
                reset($v);
                $start_time = current($v);
                $end_time   = end($v);
                $start_date = explode('-', $start_time);
                $end_date   = explode('-', $end_time);
                $start_time = date("Y-m-d", mktime(0, 0, 0, $start_date[1], $start_date[2] - 1, $start_date[0]));
                $end_time   = date("Y-m-d", mktime(0, 0, 0, $end_date[1], $end_date[2] + 1, $end_date[0]));
                $count      = 0;
                if ($post['type'] == 4) {
                    $data = Db::table('order')
                        ->alias('O')
                        ->field('O.id as order_id, O.money, O.paymoney, O.status, O.addr, O.is_user_comment, O.is_supplier_comment,O.is_complaints,
                        S.id as supplier_id, S.name as supplier_name, S.image, S.tel, ifNull(sum(Oi.tax),0) as tax')
                        ->join('order_info Oi', 'Oi.order_id = O.id')
                        ->join('supplier S', 'S.id = O.supplier_id')
                        ->where($where)
                        ->where('O.status', ['=', 4], ['=', 5], ['=', 7], 'or')
                        ->whereTime('O.send_time', '>', $start_time)
                        ->whereTime('O.send_time', '<', $end_time)
                        ->order('O.add_time desc')
                        ->group('O.id')
                        ->select();
                } else {
                    $data = Db::table('order')
                        ->alias('O')
                        ->field('O.id as order_id, O.money, O.paymoney, O.status, O.addr, O.is_user_comment, O.is_supplier_comment,O.is_complaints,
                        S.id as supplier_id, S.name as supplier_name, S.image, S.tel, ifNull(sum(Oi.tax),0) as tax')
                        ->join('order_info Oi', 'Oi.order_id = O.id')
                        ->join('supplier S', 'S.id = O.supplier_id')
                        ->where($where)
                        ->whereTime('O.send_time', '>', $start_time)
                        ->whereTime('O.send_time', '<', $end_time)
                        ->order('O.add_time desc')
                        ->group('O.id')
                        ->select();
                }
                if ($data) {
                    $count += count($data);
                    foreach ($data as $kk => $vv) {
                        $data[$kk]['tax'] = number_format($vv['tax'], 2, '.', '');
                        $product['list'] = Db::table('order')
                            ->alias('O')
                            ->field('P.name as product_name, P.image, Oi.product_id, Oi.price, Oi.specification_name,Oi.packing_proportion,Oi.unit, O.send_time,
                             P.is_include_tax,Oi.price * Oi.num as total_price,Oi.tax,Oi.num')
                            ->join('order_info Oi', 'Oi.order_id = O.id')
                            ->join('product P', 'P.id = Oi.product_id')
                            ->where(['O.id' => $vv['order_id']])
                            ->order('O.add_time desc')
                            ->select();
                        foreach ($product['list'] as $kkk => $vvv) {
                            $product['list'][$kkk]['price']         = number_format($vvv['price'], 2, '.', '');
                            $product['list'][$kkk]['total_price']   = number_format($vvv['total_price'], 2, '.', '');
                            if ($product['list'][$kkk]['is_include_tax'] == 1) {
                                $product['list'][$kkk]['tax']       = number_format($vvv['tax'], 2, '.', '');
                            } else {
                                $product['list'][$kkk]['tax']       = '--';
                            }
                        }
                        $product['count']        = count($product['list']);
                        $data[$kk]['product']    = $product;
                    }
                }
                $start = date("m.d",strtotime($start_time));
                $end   = date("m.d",strtotime($end_time));
                $time  =  $start.'-'.$end;
                if ($k == 0){
                    $order['本周'] = $data;
                }elseif ($k == 1){
                    $order['下周'] = $data;
                }else{
                    $order[$time]  = $data;
                }
            }
            if ($order) {
                $result = Config::get('code.success');
                $result['order'] = $order;
                $result['count'] = $count;
                $result['where'] = $where;

            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    public function myDealOrder()
    {
        $post = Request::instance()->post();
        if (!empty($post['user_id'])) {
            $where = ['O.user_id' => $post['user_id'], 'O.status' => ['<', 3]];
            $date = date("Y-m-d-W", time());
            $date = explode('-', $date);
            for ($i = 0; $i < 4; $i++) {
                $week = date("Y-W", mktime(0, 0, 0, $date[1], $date[2] + $i * 7, $date[0]));
                $week = explode('-', $week);
                $time[] = $this->weekday($week[0], $week[1]);
            }
            $count      = 0;
            foreach ($time as $k=>$v){
                reset($v);
                $start_time = current($v);
                $end_time   = end($v);
                $start_date = explode('-', $start_time);
                $end_date   = explode('-', $end_time);
                $start_time = date("Y-m-d", mktime(0, 0, 0, $start_date[1], $start_date[2] - 1, $start_date[0]));
                $end_time   = date("Y-m-d", mktime(0, 0, 0, $end_date[1], $end_date[2] + 1, $end_date[0]));
                $data = Db::table('order')
                    ->alias('O')
                    ->join('order_info Oi', 'Oi.order_id = O.id')
                    ->join('supplier S', 'S.id = O.supplier_id')
                    ->where($where)
                    ->whereTime('O.send_time', '>', $start_time)
                    ->whereTime('O.send_time', '<', $end_time)
                    ->order('O.add_time desc')
                    ->group('O.id')
                    ->count();
                if ($data) {
                    $count += $data;
                }
            }
            $result = Config::get('code.success');
            $result['count'] = $count;
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-待评价-评价
     * @param:
     * @return json
     */
    public function myOrderEvaluated()
    {
        $data = Request::instance()->post();
        if (!empty($data['order_id'])) {
            $order = Db::name('order')->where('id', $data['order_id'])->find();
            $supplier_comment = [];
            if ($order['is_user_comment'] == 1) {
                $list = Db::table('order_info')
                    ->alias('Oi')
                    ->field('Oi.order_id,P.id as product_id, P.name as product_name, P.image,C.star,C.comment')
                    ->join('product P', 'P.id = Oi.product_id')
                    ->join('comment C', 'C.product_id = P.id')
                    ->group('Oi.product_id')
                    ->where('C.order_id', $data['order_id'])
                    ->where(['C.comment_type' => 0])
                    ->where(['Oi.order_id' => $data['order_id']])
                    ->select();
                $supplier_comment = Db::name('comment_to_supplier')
                    ->where('order_id', $order['id'])
                    ->find();
            } else {
                $list = Db::table('order_info')
                    ->alias('Oi')
                    ->field('Oi.order_id,P.id as product_id, P.name as product_name, P.image')
                    ->join('product P', 'P.id = Oi.product_id')
                    ->group('Oi.product_id')
                    ->where(['Oi.order_id' => $data['order_id']])
                    ->select();
            }
            $orders['count'] = count($list);
            $orders['list'] = $list;
            $orders['supplier_comment'] = $supplier_comment;
            $orders['is_user_comment']  = $order['is_user_comment'];
            if ($orders['list']) {
                $result = Config::get('code.success');
                $result['order'] = $orders;
            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-待评价-评价-提交评价
     * @param:
     * @return json
     */
    public function confirmEvaluated()
    {
        $data = Request::instance()->post();
        $key = ['user_id', 'supplier_id', 'order_id', 'store_star', 'description_star', 'product',
            'logistics_services_star', 'service_attitude_star'];
        if (empty($data)) return json(Config::get('code.error'));
        foreach ($key as $k => $v) {
            if (!array_key_exists($v, $data) || !$data[$v]) {
                return json(Config::get('code.mistake'));
            }
        }
        //店铺评分
        $info1 = [
            'user_id' => $data['user_id'],
            'supplier_id' => $data['supplier_id'],
            'order_id' => $data['order_id'],
            'comment' => $data['comment'],
            'store_star' => $data['store_star'],
            'description_star' => $data['description_star'],
            'logistics_services_star' => $data['logistics_services_star'],
            'service_attitude_star' => $data['service_attitude_star'],
            'time' => date("Y/m/d H:i:s", time()),
            'flag' => 1
        ];
        Db::startTrans();
        try {
            Db::table('comment_to_supplier')->insert($info1);
            // 更新supplies的店铺评分
            $count = Db::name('comment_to_supplier')->where(['supplier_id'=>$data['supplier_id']])->count();
            $now_number = Db::name('supplier')->where(['id'=>$data['supplier_id']])->value('star');
            $total = $now_number * ($count + 1) + $data['store_star'];
            $number = $total/($count + 2);
            if ($number > 0){
                Db::name('supplier')->where(['id'=>$data['supplier_id']])->setField('star',$number);
            }else{
                $result = Config::get('code.error');
                return json($result);
            }
            if (!empty($data['product'])) {
                $product = json_decode($data['product'], true);
                foreach ($product as $k => $v) {
                    $info2 = [
                        'user_id' => $data['user_id'],
                        'supplier_id' => $data['supplier_id'],
                        'order_id' => $data['order_id'],
                        'product_id' => $v['product_id'],
                        'comment' => $v['comment'],
                        'star' => $v['star'],
                        'time' => date("Y/m/d H:i:s", time()),
                        'comment_type' => 0,
                        'flag' => 1,
                        'status' => 1,
                    ];
                    Db::table('comment')->insert($info2);
                }
            }
            Db::table('order')->where('id',$data['order_id'])->setField('is_user_comment',1);
            Db::commit();
            $result = Config::get('code.success');
        } catch (Exception $e) {
            Db::rollback();
            $result = Config::get('code.error');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-待处理-确认收货
     * @param:
     * @return json
     */
    public function confirmReceipt()
    {
        $data = Request::instance()->post();
        if (!empty($data['order_id'])) {
            $receipt_time = date("Y-m-d H:i:s", time());
            $order = Db::table('order')->where(['id' => $data['order_id']])->update(['status' => 4, 'receipt_time' => $receipt_time]);
            $order_info = Db::table('order_info')->where(['order_id' => $data['order_id']])->select();
            if ($order_info) {
                foreach ($order_info as $k => $v) {
                    Db::table('product')->where('id', $v['product_id'])->setInc('sale_num', $v['num']);
                }
            }
            if ($order) {
                $result = Config::get('code.success');
                $result['order_id'] = $data['order_id'];
            } else {
                $result = Config::get('code.error');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-订单详情
     * @param:
     * @return json
     */
    public function orderDetails()
    {
        $data = Request::instance()->post();
        if (!empty($data['order_id'])) {
            $order = Db::table('order')
                ->alias('O')
                ->field('O.id as order_id, O.money, O.paymoney, O.addr, O.status, S.tel,
                    S.name as supplier_name, S.image,O.supplier_id')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where(['O.id' => $data['order_id']])
                ->find();
            $product = Db::table('order')
                ->alias('O')
                ->field('P.id as product_id, P.name as product_name, P.image,
                    Oi.price, Oi.specification_name, Oi.packing_proportion, Oi.unit, Oi.num, 
                    Oi.price * Oi.num as total_price')
                ->join('order_info Oi', 'Oi.order_id = O.id')
                ->join('product P', 'P.id = Oi.product_id')
                ->where(['O.id' => $data['order_id']])
                ->select();
            $order['product'] = $product;
            $order['count'] = count($product);
            if ($order['product']) {
                $order['money'] = number_format($order['money'], 2, '.', '');
                $order['paymoney'] = number_format($order['paymoney'], 2, '.', '');
                foreach ($order['product'] as $k => $v) {
                    $order['product'][$k]['price'] = number_format($v['price'], 2, '.', '');
                    $order['product'][$k]['total_price'] = number_format($v['total_price'], 2, '.', '');
                }
                $result = Config::get('code.success');
                $result['order'] = $order;
            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-运送详情
     * @param:
     * @return json
     */
    public function orderTransport()
    {
        $data = Request::instance()->post();
        if (!empty($data['order_id'])) {
            $order['list'] = Db::table('order')
                ->alias('O')
                ->field('O.id, O.addr, O.status, S.name as supplier_name, S.image, S.tel, O.add_time, O.delivery_time, O.receipt_time ')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where(['O.id' => $data['order_id']])
                ->find();
            if ($order['list']) {
                $week = array("日", "一", "二", "三", "四", "五", "六");
                if ($order['list']['add_time']) {
                    $order['list']['add_week'] = "周" . $week[date('w', strtotime($order['list']['add_time']))];
                    $order['list']['add_week_english'] = date('l', strtotime($order['list']['add_time']));
                    $order['list']['add_time'] = date('H:i', strtotime($order['list']['add_time']));
                } else {
                    $order['list']['add_week'] = null;
                    $order['list']['add_week_english'] = null;
                }
                if ($order['list']['delivery_time']) {
                    $order['list']['delivery_week'] = "周" . $week[date('w', strtotime($order['list']['delivery_time']))];
                    $order['list']['delivery_week_english'] = date('l', strtotime($order['list']['delivery_time']));
                    $order['list']['delivery_time'] = date('H:i', strtotime($order['list']['delivery_time']));
                } else {
                    $order['list']['delivery_week'] = null;
                    $order['list']['delivery_week_english'] = null;
                }
                if ($order['list']['receipt_time']) {
                    $order['list']['receipt_week'] = "周" . $week[date('w', strtotime($order['list']['receipt_time']))];
                    $order['list']['receipt_week_english'] = date('l', strtotime($order['list']['receipt_time']));
                    $order['list']['receipt_time'] = date('H:i', strtotime($order['list']['receipt_time']));
                } else {
                    $order['list']['receipt_week'] = null;
                    $order['list']['receipt_week_english'] = null;
                }
                $result = Config::get('code.success');
                $result['order'] = $order;
            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-17
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-投诉
     * @param:
     * @return json
     */
    public function confirmComplaint()
    {
        $data = Request::instance()->post();
        $key = ['order_id', 'tel', 'comment', 'image'];
        if (empty($data)) return json(Config::get('code.error'));
        foreach ($key as $k => $v) {
            if (!array_key_exists($v, $data) || !$data[$v]) {
                return json(Config::get('code.mistake'));
            }
        }
        $info['order_id'] = $data['order_id'];
        $info['tel'] = $data['tel'];
        $info['comment'] = $data['comment'];
        $info['image'] = $data['image'];
        $info['add_time'] = date("Y-m-d H:i:s", time());
        Db::startTrans();
        try {
            if (is_array($info['image'])) {
                $info['image_ids'] = '';
                foreach ($info['image'] as $k => $v) {
                    Db::table('image')
                        ->data(['name' => '投诉', 'image_url' => $v, 'create_time' => $info['add_time']])
                        ->insert();
                    $info['image_ids'] .= ',' . Db::table('image')->getLastInsID();
                }
                $info['image_ids'] = ltrim($info['image_ids'], ",");
            } else {
                Db::table('image')
                    ->data(['name' => '投诉', 'image_url' => $info['image'], 'create_time' => $info['add_time']])
                    ->insert();
                $info['image_ids'] = Db::table('image')->getLastInsID();
            }
            unset($info['image']);
            $order = Db::table('order')->field('supplier_id')->where('id', $info['order_id'])->find();
            $info['supplier_id'] = $order['supplier_id'];
            Db::table('complaint')->insert($info);
            Db::table('order')->where('id',$data['order_id'])->setField('is_complaints',1);
            Db::commit();
            $result = Config::get('code.success');
        } catch (Exception $e) {
            Db::rollback();
            $result = Config::get('code.error');
        }

        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-18
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-删除订单
     * @param:
     * @return json
     */
    public function delOrder()
    {
        $data = Request::instance()->post();
        if (!empty($data['order_id'])) {
            $order = Db::table('order')->where('id', $data['order_id'])->setField('status', 6);
            if ($order) {
                $result = Config::get('code.success');
            } else {
                $result = Config::get('code.error');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    /**
     * @writer:wuqiubai
     * @DateTime:2018-01-16
     * @rewriter:
     * @rewriter_date:
     * @description:我的订单-(全部,待处理,待发货,待确认,待评价,交易关闭)
     * @param:
     * @return json
     */
    public function searchOrder()
    {
        $data = Request::instance()->post();
        if (!empty($data['user_id']) && !empty($data['page_no'])) {
            $start_time = strtotime($data['start_time']);
            $end_time = strtotime($data['end_time']) + 24 * 3600;

            $where = ['O.user_id' => $data['user_id']];
            $pageNo = $data['page_no'];
            $pageSize = 5;
            $order['list'] = Db::table('order')
                ->alias('O')
                ->field('O.id as order_id, O.money, O.paymoney, S.id as supplier_id, S.name as supplier_name, S.image,S.tel,O.status, ifNull(sum(Oi.tax),0) as tax')
                ->join('order_info Oi', 'Oi.order_id = O.id')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where($where)
                ->whereTime('O.add_time', '>=', $start_time)
                ->whereTime('O.add_time', '<=', $end_time)
                ->limit(($pageNo - 1) * $pageSize, $pageSize)
                ->order('O.add_time desc')
                ->group('O.id')
                ->select();
            $order['count'] = count($order['list']);
            $total_count = Db::table('order')
                ->alias('O')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where($where)
                ->count();
            if ($order['list']) {
                foreach ($order['list'] as $k => $v) {
                    $order['list'][$k]['tax'] = number_format($v['tax'], 2, '.', '');
                    $order['list'][$k]['money'] = number_format($v['money'], 2, '.', '');
                    $order['list'][$k]['paymoney'] = number_format($v['paymoney'], 2, '.', '');
                    $product['list'] = Db::table('order')
                        ->alias('O')
                        ->field('P.name as product_name, P.image, Oi.product_id, Oi.price, Oi.num, P.is_include_tax,
                        Oi.price * Oi.num as total_price,Oi.tax')
                        ->join('order_info Oi', 'Oi.order_id = O.id')
                        ->join('product P', 'P.id = Oi.product_id')
                        ->where(['O.id' => $v['order_id']])
                        ->select();
                    foreach ($product['list'] as $kk => $vv) {
                        $product['list'][$kk]['price'] = number_format($vv['price'], 2, '.', '');
                        $product['list'][$kk]['total_price'] = number_format($vv['total_price'], 2, '.', '');
                        if ($product['list'][$kk]['is_include_tax'] == 1) {
                            $product['list'][$kk]['tax'] = number_format($vv['tax'], 2, '.', '');
                        } else {
                            $product['list'][$kk]['tax'] = '--';
                        }
                    }
                    $product['count'] = count($product['list']);
                    $order['list'][$k]['product'] = $product;
                }
                $result = Config::get('code.success');
                $result['order'] = $order;
                $result['order']['total_count'] = $total_count;
            } else {
                $result = Config::get('code.null');
            }
        } else {
            $result = Config::get('code.mistake');
        }
        return json($result);
    }

    // 获取周始末时间
    private function weekday($year, $week)
    {
        $year_start = mktime(0, 0, 0, 1, 1, $year);
        // 判断第一天是否为第一周的开始
        if (intval(date('W', $year_start)) === 1) {
            $start = $year_start;//把第一天做为第一周的开始
        } else {
            $week++;
            $start = strtotime('+1 monday', $year_start);//把第一个周一作为开始
        }
        // 第几周的开始时间
        if ($week === 1) {
            $date['start'] = date("Y-m-d", $start);
        } else {
            $date['start'] = date("Y-m-d", strtotime('+' . ($week - 0) . ' monday', $start));
        }
        $se = [];
        $se[] = $date['start'];
        $start = explode('-', $date['start']);
        for ($i = 1; $i < 7; $i++) {
            $se[] = date("Y-m-d", mktime(0, 0, 0, $start[1], $start[2] + $i, $start[0]));
        }
        return $se;
    }

    // 获取各类订单
    private function orderInfo($userId, $type){
        $end_time   = date("Y-m-d", time());
        $date       = explode('-', $end_time);
        $start_time = date("Y-m-d", mktime(0, 0, 0, $date[1], $date[2] - 32, $date[0]));
        $end_time   = date("Y-m-d", mktime(0, 0, 0, $date[1], $date[2] + 1, $date[0]));
        $count      = 0;
        if ($type == 4) {
            $data = Db::table('order')
                ->alias('O')
                ->field('O.id as order_id, O.money, O.paymoney, O.status, O.addr, O.is_user_comment, O.is_supplier_comment,O.is_complaints, 
                S.id as supplier_id, S.name as supplier_name, S.image, S.tel, ifNull(sum(Oi.tax),0) as tax')
                ->join('order_info Oi', 'Oi.order_id = O.id')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where('O.user_id',$userId)
                ->where('O.status', ['=', 4], ['=', 5], ['=', 7], 'or')
                //->whereTime('O.receipt_time', '>', $start_time)
                //->whereTime('O.receipt_time', '<', $end_time)
                ->order('O.receipt_time desc')
                ->group('O.id')
                ->select();
        } else {
            $data = Db::table('order')
                ->alias('O')
                ->field('O.id as order_id, O.money, O.paymoney, O.status, O.addr, O.is_user_comment, O.is_supplier_comment,O.is_complaints,
                S.id as supplier_id, S.name as supplier_name, S.image, S.tel, ifNull(sum(Oi.tax),0) as tax')
                ->join('order_info Oi', 'Oi.order_id = O.id')
                ->join('supplier S', 'S.id = O.supplier_id')
                ->where('O.user_id',$userId)
                ->where('O.status',$type)
                ->whereTime('O.delete_time', '>=', $start_time)
                ->whereTime('O.delete_time', '<=', $end_time)
                ->order('O.add_time desc')
                ->group('O.id')
                ->select();
        }
        if ($data) {
            $count = count($data);
            foreach ($data as $kk => $vv) {
                $data[$kk]['tax'] = number_format($vv['tax'], 2, '.', '');
                $product['list'] = Db::table('order')
                    ->alias('O')
                    ->field('P.name as product_name, P.image, Oi.product_id, Oi.price, Oi.specification_name, O.send_time,Oi.unit,
                            Oi.packing_proportion, P.is_include_tax,Oi.price * Oi.num as total_price,Oi.tax,Oi.num')
                    ->join('order_info Oi', 'Oi.order_id = O.id')
                    ->join('product P', 'P.id = Oi.product_id')
                    ->where(['O.id' => $vv['order_id']])
                    ->select();
                foreach ($product['list'] as $kkk => $vvv) {
                    $product['list'][$kkk]['price']         = number_format($vvv['price'], 2, '.', '');
                    $product['list'][$kkk]['total_price']   = number_format($vvv['total_price'], 2, '.', '');
                    if ($product['list'][$kkk]['is_include_tax'] == 1) {
                        $product['list'][$kkk]['tax']       = number_format($vvv['tax'], 2, '.', '');
                    } else {
                        $product['list'][$kkk]['tax']       = '--';
                    }
                }
                $product['count']        = count($product['list']);
                $data[$kk]['product']    = $product;
            }
        }
        $re['data']  = $data;
        $re['count'] = $count;
        return $re;
    }

}
